package com.ybdz.scanner.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.ybdz.scanner.R;
import com.ybdz.scanner.base.BaseActivity;
import com.ybdz.scanner.bean.TakeInventoryBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: ErrorProductActivity
 * @author: zhao
 * @Description: 扫描错误的界面
 * @date 2018/2/24 17:37
 */
public class ErrorProductActivity extends BaseActivity {

    @BindView(R.id.ll_back)
    LinearLayout mLlBack;
    @BindView(R.id.home)
    FrameLayout mHome;
    @BindView(R.id.name)
    TextView mName;
    @BindView(R.id.place)
    TextView mPlace;
    @BindView(R.id.breed)
    TextView mBreed;
    @BindView(R.id.rank)
    TextView mRank;
    @BindView(R.id.batch)
    TextView mBatch;
    @BindView(R.id.birth_date)
    TextView mBirthDate;
    @BindView(R.id.specification)
    TextView mSpecification;
    @BindView(R.id.weight)
    TextView mWeight;
    @BindView(R.id.conformity)
    TextView mConformity;
    @BindView(R.id.bt_back)
    Button mBtBack;
    @BindView(R.id.empty)
    QMUIEmptyView mEmpty;

    private TakeInventoryBean.DataBean data;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_error);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        mEmpty.show(true);
        if (data != null){
            initData();
            mEmpty.hide();
        }else {
            mEmpty.show(false,"获取不到该单品信息",null,null,null);
        }
    }

    /**
     * EventBus传递的数据
     *
     * @param data
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getData(TakeInventoryBean.DataBean data) {
        this.data = data;
    }

    private void initData() {
        mName.setText("品名：" + data.getProductName());
        mPlace.setText("原产地：" + data.getOrigin());
        mBreed.setText("品种：" + data.getVarieties());
        mBatch.setText("产品批次：" + data.getProductBatch());
        mRank.setText("等级：" + data.getGrade());
        mBirthDate.setText("生产日期：" + data.getProducedDate());
        mSpecification.setText("规格：" + data.getSpecification());
        double weight = Double.parseDouble(data.getActualWeight() + "") / 1000;
        mWeight.setText("实际重量：" + weight + " Kg");
        boolean isQualified = data.isIsQualified();
        if (isQualified == true) {
            mConformity.setText("是否合格：合格");
        } else {
            mConformity.setText("是否合格：不合格");
        }
    }


    @OnClick({R.id.ll_back, R.id.home, R.id.bt_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:              //返回
                finish();
                break;
            case R.id.home:                 //返回首页
                startActivity(new Intent(ErrorProductActivity.this, MainActivity.class));
                finish();
                break;
            case R.id.bt_back:              //返回
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
